//
//  JDToastView.swift
//  readtool
//
//  Created by WanJiading on 16/6/12.
//  Copyright © 2016年 DAD. All rights reserved.
//

import UIKit

class JDToastView: UIView {
    let ScreenMargin: CGFloat                    = 60.0      // 屏幕俩边最大边距
    let TextMargin: CGFloat                      = 14.0      // 文本四周最大边距
    let BackgroundViewMinHeight: CGFloat         = 52.0      // 视图的最小高度

    convenience init(message: String) {
        self.init()

        let screenSize = MainScreen.bounds.size

        let messageLabel: UILabel = UILabel()
        messageLabel.numberOfLines = 0
        messageLabel.text = message
        messageLabel.textAlignment = NSTextAlignment.center
        messageLabel.font = UIFont.systemFont(ofSize: 16)
        messageLabel.textColor = UIColor.white
        messageLabel.sizeToFit()
        self.addSubview(messageLabel)
        let maxWidth: CGFloat = MainScreen.bounds.size.width - ScreenMargin * 2
        let margin: CGFloat = TextMargin * 2
        var width: CGFloat = maxWidth
        if maxWidth < messageLabel.bounds.size.width {
            messageLabel.frame = CGRect(x: 0, y: 0, width: maxWidth - margin, height: 0)
            messageLabel.sizeToFit()
        }
        else {
            width = messageLabel.frame.size.width + margin
        }
        var height: CGFloat = messageLabel.frame.size.height + TextMargin * 2
        if height < BackgroundViewMinHeight {
            height = BackgroundViewMinHeight
        }
        self.frame = CGRect(x: 0, y: 0, width: width, height: height)
        self.layer.cornerRadius = 4
        self.alpha = 1
        self.frame = CGRect(x: (screenSize.width - width) * 0.5, y: (screenSize.height - height) * 0.5 - 50, width: width, height: height)
        self.backgroundColor = UIColor.rgb("444444", alpha: 1)
        messageLabel.center = CGPoint(x: width * 0.5, y: height * 0.5)
    }
}
